package com.jing21.learn.common.learn;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * Created by 郑靖 on 2017/9/22.
 */
@Data
@ApiModel("公用的返回对象")
@NoArgsConstructor
@AllArgsConstructor
public class ApiResult<T> {

    public static <T> ApiResult<T> getResult(T result) {
        return new ApiResult(result);
    }

    /**
     * 获取错误
     *
     * @param errmasg 错误信息
     * @return
     */
    public static ApiResult getError(String errmasg) {
        return new ApiResult(null, SERVER_ERROR_CODE, errmasg);
    }

    /**
     * 获取错误
     *
     * @param code    错误码
     * @param errmasg 错误信息
     * @return
     */
    public static ApiResult getError(Long code, String errmasg) {
        return new ApiResult(null, code, errmasg);
    }

    private static final Long SUCCESS_CODE = 0L;
    private static final Long SERVER_ERROR_CODE = 1000L;

    private static final String SUCCESS_MESSAGE = "success";

    public ApiResult(T data) {
        this.data = data;
    }

    @ApiModelProperty("返回对象body")
    T data;

    @ApiModelProperty(value = "返回code 0成功 其他 异常", example = "0")
    private Long code = SUCCESS_CODE;

    @ApiModelProperty(value = "返回信息 success 成功", example = "服务异常")
    private String message = SUCCESS_MESSAGE;


}
